/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bankaccount;

import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author INEZ
 */
public class AdminPage extends javax.swing.JFrame {

    /**
     * Creates new form AdminPage
     */
    public AdminPage() {
        // รหัสเข้าใช้งานหน้า admin จากหน้า Login id : admin , pass : adminsit
        setResizable(false);
        initComponents();
        setVisible(true);
        lstBa = BankAccount.showAllId();
        getAccount();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        shAccId = new javax.swing.JTextField();
        shAccPss = new javax.swing.JTextField();
        shAccName = new javax.swing.JTextField();
        shAccBal = new javax.swing.JTextField();
        jtFirst = new javax.swing.JButton();
        jtPrevious = new javax.swing.JButton();
        jtLast = new javax.swing.JButton();
        jtNext = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        editMenu = new javax.swing.JMenu();
        saveMenu = new javax.swing.JMenu();
        saveMenu1 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(".:: Admin Manager ::.");

        jTabbedPane1.setBackground(new java.awt.Color(255, 204, 51));

        jPanel1.setBackground(new java.awt.Color(255, 204, 102));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setText("Password:");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel2.setText("Account ID:");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel3.setText("Name:");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel4.setText("Balance:");

        shAccId.setEditable(false);

        shAccPss.setEditable(false);

        shAccName.setEditable(false);

        shAccBal.setEditable(false);

        jtFirst.setText("|<");
        jtFirst.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                first(evt);
            }
        });

        jtPrevious.setText("<");
        jtPrevious.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                previous(evt);
            }
        });

        jtLast.setText(">|");
        jtLast.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                last(evt);
            }
        });

        jtNext.setText(">");
        jtNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                next(evt);
            }
        });

        jButton1.setText("Delete");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteAcc(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(69, 69, 69)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(shAccId)
                            .addComponent(shAccPss)
                            .addComponent(shAccName)
                            .addComponent(shAccBal, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(58, 58, 58)
                        .addComponent(jtFirst)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtPrevious)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jtNext)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtLast)))
                .addContainerGap(68, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(shAccId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(shAccPss, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(shAccName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(shAccBal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtNext)
                    .addComponent(jtLast)
                    .addComponent(jtPrevious)
                    .addComponent(jtFirst)
                    .addComponent(jButton1))
                .addGap(33, 33, 33))
        );

        jTabbedPane1.addTab("Account Manage", jPanel1);

        editMenu.setText("Edit");
        editMenu.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                ClickEdit(evt);
            }
        });
        jMenuBar1.add(editMenu);

        saveMenu.setText("Save");
        saveMenu.setEnabled(false);
        saveMenu.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                clickSave(evt);
            }
        });
        jMenuBar1.add(saveMenu);

        saveMenu1.setText("Log off");
        saveMenu1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                LogOff(evt);
            }
        });
        jMenuBar1.add(saveMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 1, Short.MAX_VALUE)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-416)/2, (screenSize.height-342)/2, 416, 342);
    }// </editor-fold>//GEN-END:initComponents

    private void ClickEdit(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ClickEdit
        shAccName.setEditable(true);
        shAccPss.setEditable(true);
        shAccBal.setEditable(true);
        saveMenu.setEnabled(true);
        editMenu.setEnabled(false);
    }//GEN-LAST:event_ClickEdit

    private void previous(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_previous
        if(itemNo <= 0){
            JOptionPane.showMessageDialog(rootPane, "This is first record");
        }else{
            itemNo--;
            getAccount();
        }
    }//GEN-LAST:event_previous

    private void first(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_first
        if(itemNo == 0){
            JOptionPane.showMessageDialog(rootPane, "This is first record");
        }else{
            itemNo = 0;
            getAccount();
        }
        
    }//GEN-LAST:event_first

    private void next(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_next
        if(itemNo >= lstBa.size()-1){
            JOptionPane.showMessageDialog(rootPane, "This is last record");
        }else{
            itemNo++;
            getAccount();
        }
    }//GEN-LAST:event_next

    private void last(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_last
        if(itemNo >= lstBa.size()-1){
            JOptionPane.showMessageDialog(rootPane, "This is last record");
        }else{
            itemNo = lstBa.size()-1;
            getAccount();
        }
    }//GEN-LAST:event_last

    private void clickSave(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_clickSave
        BankAccount ba = new BankAccount();
        ba.setAccId(Integer.parseInt(shAccId.getText()));
        ba.setAccPss(shAccPss.getText());
        ba.setAccName(shAccName.getText());
        ba.setAccBalance(Double.parseDouble(shAccBal.getText()));
        ba.UpdateAcc();
        
        lstBa = BankAccount.showAllId();
        shAccName.setEditable(false);
        shAccPss.setEditable(false);
        shAccBal.setEditable(false);
        saveMenu.setEnabled(false);
        editMenu.setEnabled(true);        
        getAccount();
    }//GEN-LAST:event_clickSave

    private void LogOff(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_LogOff
        new Login();
        dispose();
    }//GEN-LAST:event_LogOff

    private void deleteAcc(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteAcc
        int choice = JOptionPane.showConfirmDialog(rootPane, "You want to delete ID"+lstBa.get(itemNo).getAccId());
        if(choice == 0){
            BankAccount.DeleteAcc(lstBa.get(itemNo).getAccId());
            lstBa = BankAccount.showAllId();
            getAccount();
        }
    }//GEN-LAST:event_deleteAcc

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(AdminPage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AdminPage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AdminPage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AdminPage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AdminPage().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu editMenu;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JButton jtFirst;
    private javax.swing.JButton jtLast;
    private javax.swing.JButton jtNext;
    private javax.swing.JButton jtPrevious;
    private javax.swing.JMenu saveMenu;
    private javax.swing.JMenu saveMenu1;
    private javax.swing.JTextField shAccBal;
    private javax.swing.JTextField shAccId;
    private javax.swing.JTextField shAccName;
    private javax.swing.JTextField shAccPss;
    // End of variables declaration//GEN-END:variables
    int itemNo = 0;
    List<BankAccount> lstBa;
    public void getAccount(){
        BankAccount ba = lstBa.get(itemNo);
        shAccId.setText(ba.getAccId()+"");
        shAccPss.setText(ba.getAccPss());
        shAccName.setText(ba.getAccName());
        shAccBal.setText(ba.getAccBalance()+"");
    }
}
